package com.xbe.road.controller;

import com.xbe.common.annotation.Log;
import com.xbe.common.core.controller.BaseController;
import com.xbe.common.core.domain.AjaxResult;
import com.xbe.common.core.page.TableDataInfo;
import com.xbe.common.enums.BusinessType;
import com.xbe.common.utils.poi.ExcelUtil;
import com.xbe.equipment.domain.Equipment;
import com.xbe.equipment.service.IEquipmentService;
import com.xbe.framework.util.ShiroUtils;
import com.xbe.road.domain.Road;
import com.xbe.road.service.IRoadService;
import com.xbe.system.domain.SysUser;
import com.xbe.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 道路设置Controller
 *
 * @author liuzhiting
 * @date 2020-06-18
 */
@Controller
@RequestMapping("/road/road")
public class RoadController extends BaseController
{
    private String prefix = "road/road";

    @Autowired
    private IRoadService roadService;
    @Autowired
    private ISysUserService userService;

    @Autowired
    private IEquipmentService equipmentService;

    @RequiresPermissions("road:road:view")
    @GetMapping()
    public String road()
    {
        return prefix + "/road";
    }

    /**
     * 查询道路设置列表
     */
    @RequiresPermissions("road:road:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Road road)
    {
        startPage();
        SysUser sysUser = ShiroUtils.getSysUser();


        List<Road> list;
        if (sysUser.getUserId().longValue() == 1)
        {
            list = roadService.selectRoadList(road);

        }
        else
        {
            road.setUserId(sysUser.getUserId());
            list = roadService.selectRoadListByUserId(road);


        }


        return getDataTable(list);
    }

    /**
     * 导出道路设置列表
     */
    @RequiresPermissions("road:road:export")
    @Log(title = "道路设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Road road)
    {
        List<Road> list = roadService.selectRoadList(road);
        ExcelUtil<Road> util = new ExcelUtil<Road>(Road.class);
        return util.exportExcel(list, "road");
    }

    /**
     * 新增道路设置
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap)
    {
        List<SysUser> users = userService.selectUserAll();
        modelMap.put("users", users);
        return prefix + "/add";
    }

    /**
     * 新增保存道路设置
     */
    @RequiresPermissions("road:road:add")
    @Log(title = "道路设置", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Road road)

    {
        SysUser sysUser = userService.selectUserById(road.getUserId());
        road.setUserName(sysUser.getUserName());
        return toAjax(roadService.insertRoad(road));
    }

    /**
     * 修改道路设置
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap modelMap)
    {
        Road road = roadService.selectRoadById(id);

        List<SysUser> users = userService.selectUserAll();
        for (SysUser user : users)
        {
            if (road.getUserId() != null)
            {
                if (road.getUserId().longValue() == user.getUserId().longValue())
                {
                    user.setRoadflag(true);

                }
            }


        }
        modelMap.put("users", users);

        modelMap.put("road", road);

        return prefix + "/edit";
    }

    /**
     * 修改保存道路设置
     */
    @RequiresPermissions("road:road:edit")
    @Log(title = "道路设置", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Road road)
    {
        //修改设备管理中道路名称 id
        String roadName = road.getName();
        Long roadId = road.getId();
        List<Equipment> list = equipmentService.selectEquipmentByRoadId(roadId);
        if (road.getUserId() != null)
        {
            SysUser sysUser = userService.selectUserById(road.getUserId());
            road.setUserName(sysUser.getUserName());
        }

        for (Equipment equipment : list)
        {
            equipment.setRoadName(roadName);
            equipmentService.updateEquipment(equipment);

        }


        return toAjax(roadService.updateRoad(road));
    }

    /**
     * 删除道路设置
     */
    @RequiresPermissions("road:road:remove")
    @Log(title = "道路设置", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        //删除设备中道路信息
        List<String> listId = Arrays.asList(ids.split(","));

        for (String roadId : listId)
        {
            List<Equipment> list = equipmentService.selectEquipmentByRoadId(Long.parseLong(roadId));
            for (Equipment equipment : list)
            {
                equipment.setRoadId(null);
                equipment.setRoadName(null);
                equipmentService.updateEquipmentSetRoadInfoAsNull(equipment);

            }


        }


        return toAjax(roadService.deleteRoadByIds(ids));
    }


    @PostMapping("/lookEquimentList/{id}")
    @RequiresPermissions("road:road:equipmentList")
    @ResponseBody
    public TableDataInfo lookEquimentList(@PathVariable("id") Long id)
    {


        startPage();
        List<Equipment> equipmentList = equipmentService.selectEquipmentByRoadId(id);
        return getDataTable(equipmentList);
    }

    @GetMapping("/lookEquimentList/{id}")
    public String lookEquimentList(@PathVariable("id") Long id, ModelMap map)
    {
        map.put("roadId", id);
        return prefix + "/simList";
    }
}
